package com.ygqh.baby.service.impl;
import com.foxinmy.weixin4j.mp.model.TemplateMessageInfo;
import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgWeixinMessageMapper;
import com.ygqh.baby.model.WeixinMessageModel;
import com.ygqh.baby.po.YgWeixinMessage;
import com.ygqh.baby.po.YgWeixinMessageExample;
import com.ygqh.baby.service.WxMpMessageService;
import com.ygqh.baby.service.WxSmallMessageService;
import com.ygqh.baby.service.YgWeixinMessageService;
import com.ygqh.baby.service.wechat.model.SmallTemplateMsg;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class YgWeixinMessageServiceImpl implements YgWeixinMessageService{
	
	@Autowired
	private YgWeixinMessageMapper ygWeixinMessageMapper;
	@Autowired
	private WxMpMessageService wxMpMessageService;
	@Autowired
	private WxSmallMessageService wxSmallMessageService;
	
	@Override
	public List<YgWeixinMessage> find() {
		YgWeixinMessageExample example = new YgWeixinMessageExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygWeixinMessageMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgWeixinMessageExample example = new YgWeixinMessageExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygWeixinMessageMapper.countByExample(example);
	}

	@Override
	public YgWeixinMessage findById(Long id) {
		return ygWeixinMessageMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgWeixinMessage ygWeixinMessage) {
		return ygWeixinMessageMapper.insertSelective(ygWeixinMessage);
	}

	@Override
	public int update(YgWeixinMessage ygWeixinMessage) {
		return ygWeixinMessageMapper.updateByPrimaryKeySelective(ygWeixinMessage);
	}

	@Override
	public ResultSet<YgWeixinMessage> search(QueryInfo queryInfo,String q) {
		List<YgWeixinMessage>  list =  ygWeixinMessageMapper.selectSuper(queryInfo, q);
		int count = ygWeixinMessageMapper.countSuper(q);
		ResultSet<YgWeixinMessage> resultSet = new ResultSet<YgWeixinMessage>(count,list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgWeixinMessageExample example = new YgWeixinMessageExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgWeixinMessage record = new YgWeixinMessage();
		record.setStatus(DataStatus.Delete);

		return ygWeixinMessageMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(YgWeixinMessage ygWeixinMessage) {
		int r = 0;
		if(ygWeixinMessage.getId() != null) {
			r = this.update(ygWeixinMessage);
		}else {
			r = this.save(ygWeixinMessage);
		}
		return r;
	}

	@Override
	public List<WeixinMessageModel> findWeixinMessageList() {
		List<TemplateMessageInfo> templateMessageList = wxMpMessageService.getTemplateMessageList();
		if (CollectionUtils.isEmpty(templateMessageList)) {
			return new ArrayList<>();
		}
		List<WeixinMessageModel> modelList = new ArrayList<>();
		templateMessageList.stream().forEach(o -> {
			WeixinMessageModel model = new WeixinMessageModel(o.getTitle(), o.getTemplateId());
			model.setKeywordData(breakUpContent(o.getContent(), "："));
			modelList.add(model);
		});
		return modelList;
	}

	private List<String> breakUpContent(String content, String separator) {
		List<String> keywordData = new ArrayList<>();
		if (StringUtils.isBlank(content)) {
			return keywordData;
		}
		String[] split = content.split("\\n");
		if (split == null || split.length == 0) {
			return keywordData;
		}
		for (int i = 0; i < split.length; i++) {
			String[] split2 = split[i].split(separator);
			if (split2 == null || split2.length == 1) {
				continue;
			}
			keywordData.add(split2[0]);
		}
		return keywordData;
	}

	@Override
	public YgWeixinMessage findByTemplate(String templateTitle) {
		if (StringUtils.isBlank(templateTitle)) {
			return null;
		}
		YgWeixinMessageExample example = new YgWeixinMessageExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name()).andTemplateTitleEqualTo(templateTitle);
		List<YgWeixinMessage> list = ygWeixinMessageMapper.selectByExample(example);
		return CollectionUtils.isEmpty(list) ? null : list.get(0);
	}

	@Override
	public List<WeixinMessageModel> findSmallMessageList(String platformNo) {
		List<WeixinMessageModel> modelList = new ArrayList<>();
		List<SmallTemplateMsg> templateMessageList = wxSmallMessageService.getSmallMsgList(platformNo);
		if (CollectionUtils.isEmpty(templateMessageList)) {
			return modelList;
		}
		templateMessageList.forEach(o -> {
			WeixinMessageModel model = new WeixinMessageModel(o.getTitle(), o.getTemplate_id());
			model.setKeywordData(breakUpContent(o.getContent(), "\\{\\{"));
			modelList.add(model);
		});
		return modelList;
	}
}

